package 接口;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/2/17 11:41
 * @description:
 * @version:
 */
public class ConsumerTest {
    public static void main(String[] args) {
        testConsumer();
    }
    public static void testConsumer() {
        //1. 直接new一个consumer对象 ，实现里面的acce
        // pt方法
        Consumer consumer = new Consumer() {
            @Override
            public void accept(Object o) {
                System.out.println("consumer，消费者，只消费不生产");
            }
        };

        List<String> aList = Arrays.asList("13.a", "3", "5");
        aList.stream().forEach(consumer);

        //2. lambda表达式 可以返回一个consumer对象
        consumer = (s) -> System.out.println(s);

        Consumer consumer2 = new Consumer() {
            @Override
            public void accept(Object o) {
                System.out.println(o.toString());
            }
        };
        Consumer consumer3 = (x) -> System.out.println(x.toString());

        aList.stream().forEach(consumer);
        //3. 方法引用可以返回一个consumer对象
        aList.stream().forEach(System.out::println);

        //4 IntConsumer DoubleConsumer LongConsumer 等是和Consumer平级的其他几种接口，里面有accept和andThen两个方法，andThen可以连续多用多次accept内部的逻辑。
        IntConsumer intConsumer0 = s -> System.out.println(s + "intconsumer0");
        IntConsumer intConsumer1 = s -> System.out.println(s + "intconsumer1");
        intConsumer0.andThen(intConsumer1).accept(0);
    }


}
